#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "DemoArrayLoop.h"

PersonData* makePerson(char* name, char* addr, char gender, int age) {
    PersonData* rtn = (PersonData*)malloc(sizeof(PersonData));

    if (0 == strlen(name)) {
        strncpy(rtn->name, "", sizeof(rtn->name));
    } else {
        //把 src 所指向的字符串复制到 dest，最多复制 n 个字符。当 src 的长度小于 n 时，dest 的剩余部分将用空字节填充。
        strncpy(rtn->name, name, sizeof(rtn->name));
        (rtn->name)[sizeof(rtn->name) - 1] = 0;
    }
    
    if (0 == strlen(addr)) {
        strncpy(rtn->addr, "", sizeof(rtn->addr));
    } else {
        //把 src 所指向的字符串复制到 dest，最多复制 n 个字符。当 src 的长度小于 n 时，dest 的剩余部分将用空字节填充。
        strncpy(rtn->addr, addr, sizeof(rtn->addr));
        (rtn->addr)[sizeof(rtn->addr) - 1] = 0;
    }
    
    rtn->age = age;
    rtn->gender = gender;
    
    return rtn;
}

void printPersonInfo(PersonData* person) {
    printf("name:%s, age:%d, gender:%s\naddress:%s\n",
           person->name,
           person->age,
           person->gender == 0 ? "female" : "male",
           person->addr);
}

void demoArrayLoopLogic(void) {
    printf("\n\ndemoArrayLoopLogic\n");

    int a[10]={0};
    for (int i=0; i<10; ++i){
        printf("%d->%d ",i, a[i]);
    }
    printf("\nsizeof(a)=%lu\n", sizeof(a));

    //如果你想把数组元素全部初始化为一个非零值（比如1），不能使用int b[10]={1}这种方式。只会将b[0]初始化为1，而把其他元素初始化为0
    int b[10]={1};
    for (int i=0; i<10; ++i){
        printf("%d->%d ",i, b[i]);
    }
    printf("\nsizeof(b)=%lu\n", sizeof(b));

    //如果初始化列表包含数组a的所有元素，可以省略数组的长度：
    int c[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    for (int i=0; i<12; ++i){
        printf("%d->%d ",i, c[i]);
    }
    printf("\nsizeof(c)=%lu\n", sizeof(c));

    //多维数组
    int d[2][2]={{1, 2}, {3, 4}};
    for (int i=0; i<2; ++i){
        printf("size(%d)->%lu ", i, sizeof(d[i]));
        for (int j=0; j<2; ++j){
            printf("d(%d,%d)->%d ", i, j, d[i][j]);
        }
    }
    printf("\nsizeof(d)=%lu\n", sizeof(d));

    //多维数组在计算机中也是线性存储的，因此下面写法等价
    int e[2][2]={5, 6, 7, 8};
    for (int i=0; i<2; ++i){
        printf("size(%d)->%lu ", i, sizeof(e[i]));
        for (int j=0; j<2; ++j){
            printf("e(%d,%d)->%d ", i, j, e[i][j]);
        }
    }
    printf("\nsizeof(e)=%lu\n", sizeof(e));

    struct Person students[3];
    strcpy(students[0].name, "LiLei");
    students[0].age = 12;
    students[0].gender = 1;
    strcpy(students[0].addr, "class-1-1");

    strcpy(students[1].name, "HanMeiMei");
    students[1].age = 13;
    students[1].gender = 0;
    strcpy(students[1].addr, "class-1-2");

    strcpy(students[2].name, "Jim Green");
    students[2].age = 12;
    students[2].gender = 1;
    strcpy(students[2].addr, "class-1-3");

    for (int i=0; i<3; ++i){
        printf("\nname:%s, age:%d, gender:%s, address:%s", 
            students[i].name, 
            students[i].age, 
            students[i].gender==0 ? "female" : "male",
            students[i].addr);
        printf("\nsizeof(students[%d])=%lu\n", i, sizeof(students[i]));
    }
    printf("\nsizeof(students)=%lu\n", sizeof(students));
}
